﻿using Dapper;
using Dapper.Contrib.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tauren.Business.Entity.Global;
using Tauren.Business.Entity.Model;
using Tauren.Business.Entity.Output;
using Tauren.Business.Infrastructure.Globals;

namespace Tauren.Business.DLL.PositionDLL
{
    public class PositionBusiness : Base.BaseDLL, IPositionBusiness
    {
        public PositionBusiness(IConnectionDLLBase _dLLBase) : base(_dLLBase)
        {
        }

        public List<PersonInfo> GetEmployee(int PositionId)
        {
            return dbConnection.Query<PersonInfo>(@"SELECT empid,cnname,enname,0 as  onparttime FROM  employee WHERE positionId=@PositionId AND havedelete = 0 or havedelete is null
                                                    UNION  SELECT e.empid,e.cnname,e.enname,  1 as onparttime
                                                    FROM parttimeinfo p  LEFT JOIN employee e ON  e.empid = p.empid where (e.havedelete = 0 or e.havedelete is null) AND p.positionId=@PositionId", new Entity.Model.Employee() { PositionId = PositionId+"" }).ToList();
        }

        public double GetSeq()
        {
            Position organization = this.dbConnection.QueryFirstOrDefault<Position>("SELECT * FROM position WHERE havedelete=0  ORDER BY positionId DESC LIMIT 0,1");
            if (organization is null) return 1;
            else return (organization.Sequence is null ? 1 : organization.Sequence.Value + 1);
        }

        public bool IsPermissionCodeRepeat(long AutoId, string PosCode)
        {

            string sql = $"SELECT * FROM position WHERE positioncode='{PosCode}'";
            if (AutoId != 0)
            {
                sql = $"{sql} AND positionId != {AutoId}";
            }
            return dbConnection.Query<Organization>(sql).Any();
        }

        public List<PagePositionTable> Pages(QueryModel queryModel, out long Total)
        {
            Total = 0;
            string where = "";
            if (!string.IsNullOrEmpty(queryModel.KeyWord))
            {
                where += $"   AND (positioncode LIKE '%{queryModel.KeyWord}%' OR cnname LIKE '%{queryModel.KeyWord}%'  OR enname LIKE '%{queryModel.KeyWord}%' )";
            }
            if (string.IsNullOrEmpty(queryModel.SortField))
            {
                queryModel.SortField = "sequence";
                queryModel.IsAsc = false;
            }
            else
            {
                queryModel.SortField = queryModel.SortField.ToLower().Replace("dic_", "").Replace("dic", "").Trim();
            }
            if (queryModel.paramters.Any())
            {
                queryModel.paramters.ForEach(o =>
                {
                    where += $" AND {o.Label} like '%{o.Value}%'";
                });
            }
            if (queryModel.KeyWords.Any())
            {
                queryModel.KeyWords.ForEach(o =>
                {
                    where += $" AND {o.Label}='{o.Value}'";
                });
            }
            if (!queryModel.UseDisabledData)
            {
                where += $" AND enable=1  ";
            }
            if (queryModel.ExceptDataField.Any())
            { 
                queryModel.ExceptDataField.ForEach(o =>
                {
                    where += $" AND {o.Label}!='{o.Value}'";
                });
            }
            string asc = queryModel.IsAsc ? "ASC" : "DESC";
            queryModel.Page = queryModel.Page == 0 ? 1 : queryModel.Page;
            StringBuilder sql = new StringBuilder();
            sql.Append(@$"SELECT count(positionId) from position  WHERE havedelete!=1  {where} ;");
            sql.Append(@$" SELECT positionId,positioncode,positiontype,positioncode,cnname,enname,`enable`,creator,creatdate,orgtype,orgId FROM position WHERE havedelete!=1
                        {where}  ORDER BY {queryModel.SortField} {asc} LIMIT {(queryModel.Page - 1) * queryModel.PageSize},{ queryModel.PageSize} ;");
            using (var reader = dbConnection.QueryMultiple(sql.ToString()))
            {
                Total = reader.ReadFirstOrDefault<int>();
                List<PagePositionTable> pageModules = reader.Read<PagePositionTable>().ToList();
                long RowNo = ((queryModel.Page == 0 ? 1 : queryModel.Page) - 1) * queryModel.PageSize + 1;
                pageModules.ForEach(o =>
                {
                    o.RowNo = RowNo;
                    RowNo++;
                });
                return pageModules;
            }
        }
    }
}
